/**
 * @name zhangyy-g@grandsoft.com.cn
 * @description zhangyy's code
 * Date: 15-4-17
 * Time: 下午5:54
 *
 */
(function ($) {
    var preview = {
        init: function (container, options) {
            var self = this
            self.__container = container
            self.__options = options
        },

        img: function (file) {
            var width = $(window).width()
            var url = '/images/preview/'
            var method = 'post'
            var data = {
                type: '',
                p: 0,
                s: 50,
                path: file.path  || file.filePath,
                orderby: 'lastmodifiedtime',
                ordertype: 'desc'
            }

            if (file.isLinkView) {
                url = '/d/preview/'
                data = {
                    w: 800,
                    linkid: file.linkid,
                    path: file.path
                }
                method = 'get'
            }

            $.ajax({
                url: url,
                method: method,
                dataType: 'json',
                data: data,
                success: function (data) {
                    if (data.code != 0) return false
                    var data = data.data
                    var bigimage = ''
                    if (file.isLinkView) {
                        bigimage = data.bigimage
                    } else {
                        bigimage = data.piclist[data.index].bigimage
                    }
                    var dialog = self.__dialog = new App.common.modules.Dialog({
                        title: file.fileName,
                        okText: '关&nbsp;&nbsp;闭',
                        width: width,
                        message: Mustache.render(imgTpl, {bigimage: bigimage}),
                        okClass: '',
                        isConfirm: false,
                        limitHeight: false,
                        fullScreen: true
                    })
                    var index = data.index
                    var picList = data.piclist
                    var imgEle = dialog.find('img')
                    var element = dialog.element

                    $('.mod-dialog-masklayer').click(function () {
                        dialog.close()
                    })
                    touch.on(imgEle[0], 'doubletap', function (ev) {
                        element.toggleClass('preview-fullscreen')
                        dialog.reLocation()
                    })

                    if (picList && picList.length > 0) {
                        touch.on(imgEle[0], 'swiperight', function (ev) {
                            if (index > 0) {
                                dialog.find('.content').css({
                                    height: 'auto'
                                })
                                index--
                                imgEle.attr('src', picList[index].bigimage)
                                dialog.find('.title').text(picList[index].fileName)
                            }
                        })

                        touch.on(imgEle[0], 'swipeleft', function (ev) {
                            if (index < picList.length - 2) {
                                dialog.find('.content').css({
                                    height: 'auto'
                                })
                                index++
                                imgEle.attr('src', picList[index].bigimage)
                                dialog.find('.title').text(picList[index].fileName)
                            }
                        })
                    }

                    imgEle.bind('load', function () {
                        dialog.find('.content').height($(this).height())
                        dialog.setWidth($(this).width())
                    })

                }
            })
        },
        office: function (file) {
            var width = $(window).width()
            var height = $(window).height() - 86
            var url = '/review/file/previewowa/iframe/'
            var data = {
                type: '',
                p: 0,
                s: 50,
                path: file.path || file.filePath,
                orderby: 'lastmodifiedtime',
                ordertype: 'desc'
            }

            if (file.isLinkView) {
                url = '/review/file/previewowa/iframe4mobile/'
                data = {
                    type: '',
                    p: 0,
                    s: 50,
                    path: file.path || file.filePath,
                    orderby: 'lastmodifiedtime',
                    ordertype: 'desc',
                    linkid: file.linkid
                }
            }

            $.ajax({
                url: url,
                //url: '/review/file/previewowa/',
                method: 'post',
                dataType: 'json',
                data: data,
                success: function (data) {
		    var tmpdata = data.data
                    if(typeof(tmpdata) == "undefined"){
                        App.common.modules.smallnote(data.message, {
                            pattern: 'error'
                        })
                    }
                    var dialog = self.__dialog = new App.common.modules.Dialog({
                        title: file.fileName,
                        okText: '关&nbsp;&nbsp;闭',
                        width: width,
                        height: height,
                        limitHeight: false,
                        message: Mustache.render(officeTpl, {officeUrl: tmpdata.downloadurl}),
                        //message: Mustache.render(officeTpl, {officeUrl: 'http://baidu.com'}),
                        okClass: '',
                        isConfirm: false
                    })
                    dialog.find('iframe').height(height)
                }
            })
        }
    }

    var imgTpl = '<div class="mod-img"><img src="{{bigimage}}" /></div>'
    var officeTpl = '' +
        '<div class="mod-office">' +
        '   <iframe src="{{officeUrl}}" width="100%" height="100%" frameborder="0" background-color="transparent"></iframe>' +
        '</div>'
    App.modules.preview = preview
})(jQuery)
